using System;
using Gtk;
using System.Data;
using MySql.Data.MySqlClient;
using System.Reflection;

public class MainWindow: Gtk.Window
{	
	protected Gtk.TreeView tree;
	protected Gtk.Entry filterEntry;
	protected Gtk.TreeModelFilter filter;
	protected Gtk.Label Name;
	protected Gtk.Label HomeNumber;
	protected Gtk.Label HomeNumberLabel;
	protected Gtk.Label MobileNumber;
	protected Gtk.Label MobileNumberLabel;
	protected Gtk.Label PersonalEmail;
	protected Gtk.Label PersonalEmailLabel;
	protected Gtk.Label WorkEmail;
	protected Gtk.Label WorkEmailLabel;
	protected Gtk.Label Website;
	protected Gtk.Label WebsiteLabel;
	protected Gtk.Label Notes;
	protected Gtk.Label NotesLabel;
	protected Gtk.Label Address;
	protected Gtk.Label AddressLabel;
	
	protected string Note = null;
	protected string Addy = null;
	
	public MainWindow (): base ("")
	{
		Stetic.Gui.Build (this, typeof(MainWindow));
		
		this.SetSizeRequest(800,500);

		tree.AppendColumn ("Name", new Gtk.CellRendererText (), "text", 0);
		
		PopulateStore();
		
		tree.Selection.Changed += OnSelectionChanged;
		
		tree.GrabFocus();
	}
	
	public void PopulateStore()
     {
     	Gtk.ListStore musicListStore = new Gtk.ListStore (typeof (string), typeof (string), typeof (string), typeof (int), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string));
     	
        string connectionString =
          "Server=localhost;" +
          "Database=GNA;" +
          "User ID=root;" +
          "Password=;" +
          "Pooling=false";
       IDbConnection dbcon;
       dbcon = new MySqlConnection(connectionString);
       dbcon.Open();
       IDbCommand dbcmd = dbcon.CreateCommand();
    
       string sql =
           "SELECT * " +
           "FROM People";
       dbcmd.CommandText = sql;
       IDataReader reader = dbcmd.ExecuteReader();
       while(reader.Read()) {
       
       string FirstName = (string) reader["FirstName"];
       string LastName = (string) reader["LastName"];
       int id = (int) reader["id"];
       string HomeNumber = (string) reader["HomeNumber"];
       string MobileNumber = (string) reader["MobileNumber"];
       string PersonalEmail = (string) reader["PersonalEmail"];
       string WorkEmail = (string) reader["WorkEmail"];
       string Website = (string) reader["Website"];
      // string Note = "M";
       
       if(HomeNumber == "HACKUNAMATATA")
       {
       	HomeNumber = " ";
       	
       }
       
       if(MobileNumber == "HACKUNAMATATA")
       {
       	MobileNumber = " ";
       	
       }
       if(HomeNumber == "HACKUNAMATATA")
       {
       	HomeNumber = " ";
       	
       }
       
       if(PersonalEmail == "HACKUNAMATATA")
       {
       	PersonalEmail = " ";
       	
       }
       
       if(WorkEmail == "HACKUNAMATATA")
       {
       	WorkEmail = " ";
       	
       }
       if(Website == "HACKUNAMATATA")
       {
       	Website = " ";
       	
       }
       
       if(reader["Notes"].ToString() == "")
       {
       	Note = " ";
       	
       }
       else {
        Note = reader["Notes"].ToString();
       }
       
       if(reader["Address"].ToString() == "")
       {
       	Addy = " ";
       	
       }
       else {
        Addy = reader["Address"].ToString();
       }
       
       musicListStore.AppendValues(FirstName + " " + LastName, FirstName, LastName, id, HomeNumber,MobileNumber,PersonalEmail,WorkEmail,Website,Note, Addy);
	   
       }
       // clean up
       reader.Close();
       reader = null;
       dbcmd.Dispose();
       dbcmd = null;
       dbcon.Close();
       dbcon = null;
       
       filter = new Gtk.TreeModelFilter (musicListStore, null);
		
		// Specify the function that determines which rows to filter out and which ones to display
		filter.VisibleFunc = new Gtk.TreeModelFilterVisibleFunc (FilterTree);
			
		// Assign the filter as our tree's model
		tree.Model = filter;

     }	
	
	protected void OnSelectionChanged (object o, EventArgs args)
        {
                TreeIter iter;
                TreeModel model;
 
                if (((TreeSelection)o).GetSelected (out model, out iter))
                {
                       string name = (string) model.GetValue (iter, 0);
                       Name.Text = "<span font_desc='15'><b>" + name + "</b></span>";
                       Name.UseMarkup = true;
                       
                       string home = (string) model.GetValue (iter, 4);
                       if (home == " ")
                       {
                       	HomeNumber.Hide();
                       	HomeNumberLabel.Hide();
                       } else {
                       	HomeNumber.Show();
                       	HomeNumberLabel.Show();
                       	HomeNumber.Text = home;
                       }
                       
                       string mobile = (string) model.GetValue (iter, 5);
                       if (mobile == " ")
                       {
                       	MobileNumber.Hide();
                       	MobileNumberLabel.Hide();
                       } else {
                       	MobileNumber.Show();
                       	MobileNumberLabel.Show();
                       	MobileNumber.Text = mobile;
                       }
                       
                       string pemail = (string) model.GetValue (iter, 6);
                       if (pemail == " ")
                       {
                       	PersonalEmail.Hide();
                       	PersonalEmailLabel.Hide();
                       } else {
                       	PersonalEmail.Show();
                       	PersonalEmailLabel.Show();
                       	PersonalEmail.Text = pemail;
                       }
                       
                       string wemail = (string) model.GetValue (iter, 7);
                       if (wemail == " ")
                       {
                       	WorkEmail.Hide();
                       	WorkEmailLabel.Hide();
                       } else {
                       	WorkEmail.Show();
                       	WorkEmailLabel.Show();
                       	WorkEmail.Text = pemail;
                       }
                       
                       string web = (string) model.GetValue (iter, 8);
                       if (web == " ")
                       {
                       	Website.Hide();
                       	WebsiteLabel.Hide();
                       } else {
                       	Website.Show();
                       	WebsiteLabel.Show();
                       	Website.Text = web;
                       }
                       
                       string note = (string) model.GetValue (iter, 9);
                       if (note == " ")
                       {
                       	Notes.Hide();
                       	NotesLabel.Hide();
                       } else {
                       	Notes.Show();
                       	NotesLabel.Show();
                       	Notes.Text = note;
                       }
					   
					   string addy = (string) model.GetValue (iter, 10);
                       if (addy == " ")
                       {
                       	Address.Hide();
                       	AddressLabel.Hide();
                       } else {
                       	Address.Show();
                       	AddressLabel.Show();
                       	Address.Text = addy;
                       }                       
                }
        }
	
	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}

	protected void refresh(object sender, System.EventArgs e)
	{
		PopulateStore();
	}
	
	protected virtual void newContact(object sender, System.EventArgs e)
	{
		Dialog NewContact = new People.NewContact();
		NewContact.Show();
		NewContact.Hidden += refresh;
	}

	private void OnFilterEntryTextChanged (object o, System.EventArgs args)
	{
		// Since the filter text changed, tell the filter to re-determine which rows to display
		filter.Refilter ();
	}
	
	private bool FilterTree (Gtk.TreeModel model, Gtk.TreeIter iter)
	{
		string artistName = model.GetValue (iter, 0).ToString ();
		string artistNameL = artistName.ToLower();
		string artistNameU = artistName.ToUpper();
		
 
		if (filterEntry.Text == "")
			return true;
 
		if (artistNameL.IndexOf (filterEntry.Text) > -1 || artistNameU.IndexOf (filterEntry.Text) > -1)
			return true;
		else
			return false;
	}
	
}